package com.example.shixunqq.chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class SimpleLineChart extends View {
    private List<Float> values = new ArrayList<>();
    private List<String> labels = new ArrayList<>();
    private Paint linePaint;
    private Paint textPaint;
    private Paint pointPaint;
    private float padding = 50;
    private float textSize = 30;
    private int lineColor = Color.BLUE;

    public SimpleLineChart(Context context) {
        super(context);
        init();
    }

    public SimpleLineChart(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        linePaint = new Paint();
        linePaint.setColor(lineColor);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setStrokeWidth(4f);
        linePaint.setAntiAlias(true);

        textPaint = new Paint();
        textPaint.setColor(Color.BLACK);
        textPaint.setTextSize(textSize);
        textPaint.setAntiAlias(true);

        pointPaint = new Paint();
        pointPaint.setColor(lineColor);
        pointPaint.setStyle(Paint.Style.FILL);
        pointPaint.setAntiAlias(true);
    }

    public void setData(List<Float> values, List<String> labels) {
        this.values = values;
        this.labels = labels;
        invalidate();
    }

    public void setLineColor(int color) {
        this.lineColor = color;
        linePaint.setColor(color);
        pointPaint.setColor(color);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (values.isEmpty()) return;

        float width = getWidth() - 2 * padding;
        float height = getHeight() - 2 * padding;
        float maxValue = getMaxValue();
        float minValue = getMinValue();
        float valueRange = maxValue - minValue;

        // 绘制路径
        Path path = new Path();
        float xStep = width / (values.size() - 1);
        
        for (int i = 0; i < values.size(); i++) {
            float x = padding + i * xStep;
            float y = padding + height - (values.get(i) - minValue) * height / valueRange;
            
            if (i == 0) {
                path.moveTo(x, y);
            } else {
                path.lineTo(x, y);
            }

            // 绘制数据点
            canvas.drawCircle(x, y, 8, pointPaint);

            // 绘制标签
            if (i < labels.size()) {
                String label = labels.get(i);
                float textWidth = textPaint.measureText(label);
                canvas.drawText(label, x - textWidth / 2, getHeight() - padding / 2, textPaint);
            }

            // 绘制数值
            String value = String.format("%.1f", values.get(i));
            float textWidth = textPaint.measureText(value);
            canvas.drawText(value, x - textWidth / 2, y - 20, textPaint);
        }

        canvas.drawPath(path, linePaint);
    }

    private float getMaxValue() {
        float max = Float.MIN_VALUE;
        for (Float value : values) {
            if (value > max) max = value;
        }
        return max;
    }

    private float getMinValue() {
        float min = Float.MAX_VALUE;
        for (Float value : values) {
            if (value < min) min = value;
        }
        return min;
    }
} 